package com.stud.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stud.common.exception.ServiceException;
import com.stud.common.jdbc.MyJdbcTemplate;
import com.stud.common.jdbc.SqlHelp;
import com.stud.common.mybatis.BaseServiceImpl;
import com.stud.system.domain.WorkflowTask;
import com.stud.system.mapper.WorkflowTaskMapper;
import com.stud.system.service.WorkflowTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 流程实例任务Service业务层处理
 *
 * @author Stud
 * @date 2023-03-06
 */
@Service
@RequiredArgsConstructor
public class WorkflowTaskServiceImpl extends BaseServiceImpl<WorkflowTaskMapper, WorkflowTask> implements WorkflowTaskService {
    private final MyJdbcTemplate jdbcTemplate;

    @Override
    public List<WorkflowTask> listByWorkflowId(Long workflowId) {
        SqlHelp sqlHelp = SqlHelp.init();
        sqlHelp.append(" select ");
        sqlHelp.append("     t1.id,t1.definition_task_id,t1.workflow_id,t1.business_id,t1.business_code, ");
        sqlHelp.append("     t1.task_code,t1.task_name,t1.task_type,t1.task_form_path,t1.task_form_name, ");
        sqlHelp.append("     t1.file_flag,t1.role_code,t1.user_id,t1.user_name,t1.form_default, ");
        sqlHelp.append("     t1.task_status,t1.process_time,t1.task_form_status,t1.task_info,t1.sort_num, ");
        sqlHelp.append("     t1.flag,t1.del_flag,t1.create_by,t1.create_time,t1.update_by, ");
        sqlHelp.append("     t1.update_time,t1.remark, ");
        sqlHelp.append("     t2.role_name ");
        sqlHelp.append(" from t_workflow_task t1 ");
        sqlHelp.append(" left join sys_role t2 on t1.role_code = t2.role_key ");
        sqlHelp.append(" where workflow_id = ? ", workflowId);
        sqlHelp.append(" order by sort_num ");
        return this.jdbcTemplate.select(sqlHelp, WorkflowTask.class);
    }

    @Override
    public WorkflowTask firstTask(Long workflowId, String... taskType) {
        LambdaQueryWrapper<WorkflowTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowTask::getWorkflowId, workflowId);
        if (Objects.nonNull(taskType)) {
            queryWrapper.in(WorkflowTask::getTaskType, taskType);
        }
        queryWrapper.orderByAsc(WorkflowTask::getSortNum);
        queryWrapper.last(" limit 1 ");
        return getOne(queryWrapper);
    }

    @Override
    public WorkflowTask lastTask(Long workflowId) {
        LambdaQueryWrapper<WorkflowTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowTask::getWorkflowId, workflowId);
        queryWrapper.orderByDesc(WorkflowTask::getSortNum);
        queryWrapper.last(" limit 1 ");
        return getOne(queryWrapper);
    }

    @Override
    public WorkflowTask mainTask(Long workflowId) {
        LambdaQueryWrapper<WorkflowTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowTask::getWorkflowId, workflowId);
        queryWrapper.eq(WorkflowTask::getTaskType, "2");
        queryWrapper.last(" limit 1 ");
        return getOne(queryWrapper);
    }

    @Override
    public WorkflowTask prevTask(WorkflowTask workflowTask) {
        if (Objects.isNull(workflowTask) || Objects.isNull(workflowTask.getId()) || Objects.isNull(workflowTask.getWorkflowId())) {
            throw new ServiceException("参数异常");
        }
        if (Objects.isNull(workflowTask.getSortNum())) {
            workflowTask = getById(workflowTask);
        }
        LambdaQueryWrapper<WorkflowTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowTask::getWorkflowId, workflowTask.getWorkflowId());
        queryWrapper.lt(WorkflowTask::getSortNum, workflowTask.getSortNum());
        queryWrapper.orderByDesc(WorkflowTask::getSortNum);
        queryWrapper.last(" limit 1 ");
        return getOne(queryWrapper);
    }

    @Override
    public WorkflowTask nextTask(WorkflowTask workflowTask) {
        if (Objects.isNull(workflowTask) || Objects.isNull(workflowTask.getId()) || Objects.isNull(workflowTask.getWorkflowId())) {
            throw new ServiceException("参数异常");
        }
        if (Objects.isNull(workflowTask.getSortNum())) {
            workflowTask = getById(workflowTask);
        }
        LambdaQueryWrapper<WorkflowTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowTask::getWorkflowId, workflowTask.getWorkflowId());
        queryWrapper.gt(WorkflowTask::getSortNum, workflowTask.getSortNum());
        queryWrapper.orderByAsc(WorkflowTask::getSortNum);
        queryWrapper.last(" limit 1 ");
        return getOne(queryWrapper);
    }
}
